package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemReplyMainBinding;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.ReplyDetailItem;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/9/19.
 */
public class ReplyMainViewHolder extends AbsViewHolder<ReplyDetailItem> {
    private ItemReplyMainBinding mItemBinding;

    public ReplyMainViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
        mItemBinding.tvName.setOnClickListener(this);
        mItemBinding.ivHeader.setOnClickListener(this);
    }

    @Override
    protected void onBindView(final ReplyDetailItem item) {
        final PbBean.PbUserSimple userInfo = item.userInfo;
        if (userInfo == null) return;
        mItemBinding.setIsOwner(TextUtils.equals(userInfo.getUserId(), ConfigUtils.getUserId(convertView.getContext())));
        mItemBinding.icFollowStatus.setImageResource(item.isFollowed ? R.mipmap.ic_cf_user : R.mipmap.ic_af_user);
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), mItemBinding.ivHeader);
        mItemBinding.setIsDesigner(userInfo.getIsDesigner());
        mItemBinding.tvName.setText(StringUtils.filterNullAndTrim(userInfo.getNickname()));
        mItemBinding.tvIntro.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(userInfo.getIntroduction())) ? View.GONE : View.VISIBLE);
        mItemBinding.tvIntro.setText(userInfo.getIntroduction());
        mItemBinding.tvTitle.setText(StringUtils.filterNullAndTrim(item.quizTitle));
        if (item.voteOptionType != null) {
            switch (item.voteOptionType) {
                case VOTE_OPTION_TYPE_A:
                    mItemBinding.tvVoteOption.setText("Ta选了A");
                    mItemBinding.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_B:
                    mItemBinding.tvVoteOption.setText("Ta选了B");
                    mItemBinding.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_C:
                    mItemBinding.tvVoteOption.setText("Ta选了C");
                    mItemBinding.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_D:
                    mItemBinding.tvVoteOption.setText("Ta选了D");
                    mItemBinding.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_E:
                    mItemBinding.tvVoteOption.setText("Ta选了E");
                    mItemBinding.tvVoteOption.setVisibility(View.VISIBLE);
                    break;
                case VOTE_OPTION_TYPE_NONE:
                    mItemBinding.tvVoteOption.setVisibility(View.GONE);
                    break;
            }
        } else {
            mItemBinding.tvVoteOption.setVisibility(View.GONE);
        }
        mItemBinding.llQuizTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.extraBaseQuestion != null) {
                    if (item.extraBaseQuestion.getQuestionId() > 0) {
                        EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_TITLE_ACTION_START, item.extraBaseQuestion));
                    } else {
                        EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_TITLE_ACTION_FINISH, item.extraBaseQuestion));
                    }
                } else {
                    ExtraBaseQuestion extraBaseQuestion = new ExtraBaseQuestion(item.questionId, false);
                    EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_TITLE_ACTION_START, extraBaseQuestion));
                }
            }
        });

        mItemBinding.icFollowStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBusUtil.post(new ReplyDetailEvent(item.isFollowed ? ReplyDetailEvent.EVENT_USER_CANCEL_FOLLOW : ReplyDetailEvent.EVENT_USER_ADD_FOLLOW, userInfo.getUserId()));
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_header:
            case R.id.tvName:
                UserInfoActivity.start(v.getContext(), item.userInfo.getUserId(), item.userInfo.getIsDesigner());
                break;
        }
    }
}
